/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ig.ejb.metrica.da.editor.dao;

import ig.ejb.metrica.da.editor.dao.abs.DAO;
import ig.ejb.metrica.da.session.editor.dto.TablaDTO;
import java.util.List;

/**
 *
 * @author jsanchez
 */
public class TablaDao extends DAO {

    @Override
    public Object buscar(Integer id) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID, DESCRIPCION, NOMBRE FROM TABLA WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            Object dto = null;
            if (resul.next()) {
                dto = new TablaDTO();
                getDatos(resul, dto);
            }
            return dto;
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public List buscarTodos() throws Exception {
        List c = new java.util.ArrayList();
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID, DESCRIPCION, NOMBRE FROM TABLA");
            resul = prep.executeQuery();
            while (resul.next()) {
                c.add(getDatos(resul, new TablaDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public Integer guardar(Object obj) throws Exception {
        TablaDTO dto = (TablaDTO) obj;
        try {
            getConnection();
            if (dto.getId() == null) {
                prep = connection.prepareStatement("INSERT INTO TABLA( DESCRIPCION, NOMBRE, ID) VALUES(?, ?, ?)");
                dto.setId(getMaximoId("TABLA"));
            } else {
                prep = connection.prepareStatement("UPDATE TABLA SET DESCRIPCION = ?, NOMBRE = ? WHERE ID = ?");
            }
            setDatos(prep, dto,"DESCRIPCION, NOMBRE, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } catch (Throwable ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public void eliminar(Integer id) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("DELETE FROM TABLA WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public Object existe(Object obj) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID FROM TABLA WHERE NOMBRE = ?");
            if(obj instanceof TablaDTO){
                prep.setString(1, ((TablaDTO) obj).getNombre().trim());
            }else if(obj instanceof String){
                prep.setString(1, obj.toString().trim());
            }
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
        return null;
    }

}
